home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / machserver / 1.098 / dev / sun4.md / devJaguarHBA.c < prev    next >
C/C++ Source or Header  |  1991-08-09  |  58KB  |  1,907 lines

  1. /* 
  2.  * devJaguarHBA.c --
  3.  *
  4.  *    Driver for the Interphase V/SCSI 4210 Jaguar SCSI host bus adapter 
  5.  *    for SUN's running Sprite.
  6.  *
  7.  * Copyright 1989 Regents of the University of California
  8.  * Permission to use, copy, modify, and distribute this
  9.  * software and its documentation for any purpose and without
  10.  * fee is hereby granted, provided that the above copyright
  11.  * notice appear in all copies.  The University of California
  12.  * makes no representations about the suitability of this
  13.  * software for any purpose.  It is provided "as is" without
  14.  * express or implied warranty.
  15.  */
  16.  
  17. #ifndef lint
  18. static char rcsid[] = "$Header: /sprite/src/kernel/dev/sun3.md/RCS/devJaguarHBA.c,v 9.6 91/08/09 16:30:28 mendel Exp $ SPRITE (Berkeley)";
  19. #endif /* not lint */
  20.  
  21. #include "sprite.h"
  22. #include "mach.h"
  23. #include "jaguar.h"
  24. #include "jaguarDefs.h"
  25. #include "dev.h"
  26. #include "devInt.h"
  27. #include "scsi.h"
  28. #include "scsiHBA.h"
  29. #include "scsiDevice.h"
  30. #include "vmMach.h"
  31. #include "sync.h"
  32. #include "stdio.h"
  33. #include "stdlib.h"
  34. #include "string.h"
  35. #include "bstring.h"
  36.  
  37. /*
  38.  * Until the hardware is fixed only allow one command at a time into
  39.  * the board.
  40.  */
  41. #define ONE_CMD_AT_A_TIME
  42.  
  43. /*
  44.  * WARNING -- WARNING --- WARNING --- WARNING
  45.  * The code and data structures in this file are carefully coded to 
  46.  * run on machines that don't do autobus sizing and unaligned memory fetches.
  47.  *
  48.  * Please keep in mind the following:
  49.  *
  50.  * 1) Although structures like struct JaguarMem appear to be C data structures
  51.  *    they are used to describe the interface to the Jaguar controller board.
  52.  *    Because these data structures need to be understood by both the
  53.  *    host CPU and the Jaguar's processor changes should only occur when
  54.  *    the Jaguar's firmware changes the interface. 
  55.  * 2) The Jaguar is accessed in the short IO spaces on the VME bus. This
  56.  *    means that the data path is only 16 bits wide. This means that
  57.  *    unless your processor does autobus sizing (ie convert 32 bit accesses
  58.  *    into 2 16 bit access if the bus width is 16 bit) only shorts should
  59.  *    be stored into JaguarMem. Note: SPARC (the sun4) doesn't do autobus
  60.  *    sizing.
  61.  * 3) Beware of padding introduced by the compiler. For example, if the
  62.  *    Jaguar documentation says:
  63.  *      2 bytes length
  64.  *      4 bytes buffer address
  65.  *    and you declare a C structure to be 
  66.  *       struct {
  67.  *        short length;
  68.  *        char  *bufAddr;
  69.  *     }
  70.  *    Things will break (on sun4) because the compiler will added 2 bytes of
  71.  *    padding to insure that the (char *) starts on the (4 byte) word boundry.
  72.  *    The routine CheckSizes in this file tries to catch errors of this 
  73.  *    form.
  74.  */
  75.  
  76. /*
  77.  * The Jaguar occupies 2 Kbytes on the VME short I/O space (DEV_VME_D16A16).
  78.  * The image of these 2 Kbytes is described by the structure 
  79.  * struct JaguarMem. Some of the members of this structure like 
  80.  * the mcsb, mce, cmdQueue,and css must occur at fixed offsets from the start
  81.  * of the 2K region. The other items in JaguarMem are placed at fixed offsets
  82.  * to simplify the driver and may be moved around.
  83.  */
  84.  
  85. /*
  86.  * Constants that define the layout of JaguarMem.
  87.  *
  88.  * SIZE_JAUGAR_MEM - The size of the dual ported memory accessible to the
  89.  *             host.
  90.  * NUM_CQE       -  Number of entries in the command queue. This limits
  91.  *                  the number of unacknowledged commands that we can submit
  92.  *                 to the Jaguar. For a lack of a better number, 8.
  93.  *
  94.  * NUM_SG_ELEMENTS - Number of scatter/gather elements allocated in the 
  95.  *             juguar memory.  Scatter/gather elements don't needed
  96.  *             to be allocated in Jaguar memory but it is convenient
  97.  *             to do so.  Otherwise they would have to be allocated
  98.  *             or mapped into DVMA space.
  99.  *
  100.  * MEM_PAD           Number of bytes of free space not allocated in the
  101.  *                  structure JaguarMem. Note that MEM_PAD is a function 
  102.  *                 of NUM_SG_ELEMENTS and NUM_CQE.
  103.  * NUM_WORK_QUEUES   Number of work queues to use. 
  104.  * 
  105.  */
  106. #define    SIZE_JAUGAR_MEM    (2*1024)
  107. #define    NUM_CQE 8
  108. #define    NUM_SG_ELEMENTS    64
  109. #define    NUM_WORK_QUEUES    14
  110.  
  111. #define    MEM_PAD (SIZE_JAUGAR_MEM - sizeof(JaguarMCSB) -  \
  112.         (NUM_CQE+1)*(sizeof(JaguarIOPB)+sizeof(JaguarCQE)) - \
  113.         sizeof(JaguarCRB) - sizeof(JaguarCCSB) - \
  114.         NUM_SG_ELEMENTS * sizeof(JaguarSG))
  115.  
  116. typedef struct JaguarMem {
  117.     JaguarMCSB    mcsb;    /* Master Control and Status Block. 16 Bytes  */
  118.     JaguarCQE    mce;    /* Master Command Entry Queue. 12 Bytes */
  119.     JaguarCQE    cmdQueue[NUM_CQE]; /* Command Queue. 12*NUM_CQE Bytes */
  120.     JaguarIOPB    masterIOPB;    /* IOPB used for the MCE. 64 Bytes */
  121.     JaguarIOPB    iopbs[NUM_CQE]; /* IOPB for cmdQueue. 64*NUM_CQE Bytes*/
  122.     JaguarSG    sgElements[NUM_SG_ELEMENTS];
  123.                     /* Scatter/gather elements for host. */
  124.     char        padding[MEM_PAD]; /* Padding to force css field into
  125.                        * correct offset. */
  126.     JaguarCRB    crb;    /* Command response block. 76 Bytes */
  127.     JaguarCCSB    css;    /* Contoller specific Space. 120 Bytes */
  128. } JaguarMem;
  129.  
  130. /*
  131.  * Many of the Jaguar's data structures need addresses as offsets into the
  132.  * JaguarMem. To deal with this offsets:
  133.  *
  134.  * POINTER_TO_OFFSET()    - Convert a host pointer to an offset.
  135.  * OFFSET_TO_POINTER()    - Convert an offset to a host pointer.
  136.  *
  137.  */
  138. #define    POINTER_TO_OFFSET(ptr, memPtr)    ((unsigned)(ptr) - (unsigned)(memPtr))
  139. #define OFFSET_TO_POINTER(offset, memPtr) ((char *)(memPtr)+(unsigned)(offset))
  140.  
  141. /* 
  142.  * workq (s) - 
  143.  * From the documentation it appears that for the Jaguar to  efficiently 
  144.  * talk to a device a workq must be configured for the device.  The 
  145.  * documentation doesn't mention a cost associated with having many 
  146.  * unused workq so it is tempting to create all 14 work queues at 
  147.  * initialization time.  The reason for not doing this is that certain
  148.  * options such as parity and workq priority is determined during
  149.  * work queue initialization. 
  150.  * 
  151.  */
  152. /* Forward declaration. */
  153. typedef struct Controller Controller;
  154.  
  155. /*
  156.  * Device - The data structure containing information about a device. One of
  157.  * these structure is kept for each attached device. Note that is structure
  158.  * is casted into a ScsiDevice and returned to higher level software.
  159.  * This implies that the ScsiDevice must be the first field in this
  160.  * structure.
  161.  */
  162.  
  163. typedef struct Device {
  164.     ScsiDevice handle;    /* Scsi Device handle. This is the only part
  165.              * of this structure visible to higher 
  166.              * level software. MUST BE FIRST FIELD IN STRUCTURE. */
  167.     int    bus;        /* Bus number of device. */
  168.     int    targetID;    /* TargetID of device. */
  169.     int    unitAddress;    /* Jaguar address of this device.  */
  170.     int    workQueue;    /* Jaguar workqueue allocated for this device. */
  171. #ifndef ONE_CMD_AT_A_TIME
  172.     int    numActiveCmds;    /* Number of commands enqueued on the HBA for this
  173.              * command. */
  174. #endif /* not ONE_CMD_AT_A_TIME */
  175.     Controller  *ctrlPtr; /* Controller to which device is attached. */
  176.            /*
  177.             * The following part of this structure is 
  178.             * used to handle SCSI commands that return 
  179.             * CHECK status. To handle the REQUEST SENSE
  180.             * command we must: 1) Save the state of the current
  181.             * command into the "struct FrozenCommand". 2) Submit
  182.             * a request sense command  */
  183.  
  184.     struct FrozenCommand {               
  185.     ScsiCmd    *scsiCmdPtr;       /* The frozen command. */
  186.     unsigned char statusByte; /* It's SCSI status byte, Will always have
  187.                    * the check bit set. */
  188.     int amountTransferred;    /* Number of bytes transferred by this 
  189.                    * command. */
  190.     } frozen;    
  191. } Device;
  192.  
  193. typedef struct CmdAction {
  194.     int    action;        /* Action to be performed when command completes. 
  195.              * See below for list of actions. */
  196.     int        dmaBufferLen;    /* DMA buffer length. */
  197.     Address    dmaBuffer; /* DMA buffer for device. */
  198.     ClientData    actionArg;    /* Argument for action. */
  199. } CmdAction;
  200.  
  201. /*
  202.  * Upon command termination. The interrupt handler can be instructed to
  203.  * perform serveral possible actions.
  204.  *
  205.  * UNUSED_ACTION -     This action buffer is used.
  206.  * FILL_IN_CRB_ACTION - Fill in the specified pointer with the completion
  207.  *            CRB.
  208.  * SCSI_CMD_ACTION -    This command is a SCSI command. Invoke RequestDone
  209.  *            function.
  210.  * IS_WAIT_ACTION() -   TRUE if the action requires the command to be
  211.  *            executed synchronously.
  212.  * NUM_ACTIONS        -   Number of action buffers to allocate per controller.
  213.  *            This number must be creater than the number of 
  214.  *            devices * the number of queued commands.
  215.  */
  216. #define    UNUSED_ACTION        0x0
  217. #define    FILL_IN_CRB_ACTION    0x1
  218. #define    SCSI_CMD_ACTION        0x2
  219.  
  220. #define    IS_WAIT_ACTION(action)    ((action)== FILL_IN_CRB_ACTION)
  221. #define    NUM_ACTIONS        64
  222. /*
  223.  * Controller - The Data structure describing a Jaguar controller. One
  224.  * of these structures exists for each active Jaguar HBA on the system. Each
  225.  * controller may have from zero to 14 devices attached to it. 
  226.  */
  227. struct Controller {
  228.     volatile JaguarMem *memPtr;    /* Pointer to the registers
  229.                                     of this controller. */
  230.     volatile JaguarCQE *nextCQE; /* Next available CQE. */
  231.     Boolean workQueue0Busy; /* Work Queue 0 is being used. */
  232. #ifdef ONE_CMD_AT_A_TIME
  233. #define    MAX_CMDS_IN_CONTROLLER    1
  234.     int numActiveCmds; /* Number of active commands in controller. */
  235. #endif /* ONE_CMD_AT_A_TIME */
  236.     char    *name;    /* String for error message for this controller.  */
  237.     Sync_Semaphore mutex; /* Lock protecting controller's data structures. */
  238.     DevCtrlQueues devQueues;    /* Device queues for devices attached to this
  239.                  * controller.     */
  240.     Sync_Condition ctrlCmdWait; /* Wait condition for syncronous command
  241.                  * to finish. */
  242.     Sync_Condition ctrlQueue0Wait; /* Wait condition for exclustive access
  243.                     * to workqueue 0. */
  244.     int        intrLevel;    /* VME interrupt level for controller. */
  245.     int        intrVector;    /* VME interrupt vector for controller in
  246.                  * the format expected by the Jaguar. */
  247.     int        nextActionBuffer; /* Next cmdAction buffer to allocated. */
  248.     CmdAction    cmdAction[NUM_ACTIONS]; /* Action to be performed when command 
  249.                          * completes. */
  250.     Device  *devices[NUM_WORK_QUEUES];   /* Pointers to the device attached. The
  251.                      * index is the workQueue number - 1. */
  252. };
  253.  
  254. #define MAX_JAGUAR_CTRLS    16
  255. static Controller    *Controllers[MAX_JAGUAR_CTRLS];
  256.  
  257.  
  258. static int    devJaguarDebug = 0;
  259.  
  260. /*
  261.  * The follow data structure is used by the Sprite kernel debugger to 
  262.  * examine Jaguar memory. See routine GetJaguarMem.
  263.  */
  264. #ifndef lint
  265. static JaguarMem DebugJaguarMem;
  266. #endif
  267. /*
  268.  * Constants for the sun implementation.
  269.  * DMA_BURST_COUNT - The number of VME DMA transfers performed in a 
  270.  *             single burst before releasing the bus.  A value of
  271.  *             0 uses the maximum of 128 32-bit transfers.
  272.  * JAGUAR_ADDRESS_MODIFIER - The Address space modifier and transfer type
  273.  *                 for Jaguar DMA. We choose 32 bit normal mode
  274.  *                 transfers with a A24 bit supervisor data address
  275.  *                 modifier (0x3d).
  276.  * MAX_CMDS_QUEUED - Maximum number of command to queue per device.
  277.  * SELECTION_TIMEOUT - Timeout value for selecting a device in 1 millisecond
  278.  *               ticks. We choose 1 second timeout.
  279.  * RESELECTION_TIMEOUT - Timeout value for a device re-selecting in 32 
  280.  *             milliseconds ticks. 0 means infinite.  
  281.  * VME_TIMEOUT - Timeout value for VME access. 0 means 100 milliseconds.
  282.  * DEV_MAX_DMA_SIZE - Maximum size of a DMA request to this device.
  283.  */
  284.  
  285. #define    DMA_BURST_COUNT        0
  286. #define    JAGUAR_ADDRESS_MODIFIER  (JAGUAR_32BIT_MEM_TYPE | \
  287.                   JAGUAR_NORMAL_MODE_XFER | 0x0D)
  288. #define    MAX_CMDS_QUEUED        2
  289. #define    SELECTION_TIMEOUT    1000
  290. #define    RESELECTION_TIMEOUT    0
  291. #define    VME_TIMEOUT        0
  292. #define    DEV_MAX_DMA_SIZE    (128*1024)
  293. #define    VME_INTERRUPT_PRIORITY    2
  294.  
  295.  
  296. /*
  297.  * Macros for reading and writing 32 bit values from the short IO space.
  298.  */
  299.  
  300. #define    READ_LONG(var)    (((var)[0]<<16)|((var)[1]))
  301. #define    SET_LONG(var,value) \
  302.         (((var)[0] = ((value)>>16)),((var)[1]=(0xffff&(value))))
  303.  
  304. static void LockWorkq0();
  305. static void UnLockWorkq0();
  306. static void CopyFromJaguarMem();
  307. static void CopyToJaguarMem();
  308. static void ZeroJaguarMem();
  309. static Boolean WaitForBitSet();
  310. static void FillInScsiIOPB();
  311. static void RequestDone();
  312. static void StartNextRequest();
  313. static char *ErrorString();
  314. static Boolean SendJaguarCmd();
  315.  
  316. /*
  317.  *----------------------------------------------------------------------
  318.  *
  319.  * GetJaguarMem --
  320.  *
  321.  *    Make a copy of the Jaguar's memory image in DebugJaguarMem. This
  322.  *    routine is used because the Sprite debugger can't read data
  323.  *    in short VME space. It is only called by the debugger.
  324.  *
  325.  * Results:
  326.  *    The address of the Jaguar memory copied.
  327.  *
  328.  * Side effects:
  329.  *    None.
  330.  *
  331.  *----------------------------------------------------------------------
  332.  */
  333. #ifndef lint
  334. static unsigned int
  335. GetJaguarMem(ctrlNum)
  336.     int    ctrlNum;    /* Controller to number to act upon. */
  337. {
  338.     CopyFromJaguarMem(Controllers[ctrlNum]->memPtr, &DebugJaguarMem, 2048);
  339.     return (unsigned int) Controllers[ctrlNum]->memPtr;
  340. }
  341. #endif
  342.  
  343. /*
  344.  *----------------------------------------------------------------------
  345.  *
  346.  * WaitForResponseBlock --
  347.  *
  348.  *    Wait for a Jaguar command to complete. 
  349.  *
  350.  * Results:
  351.  *    TRUE always. Should probably sent up a timeout handler.
  352.  *
  353.  * Side effects:
  354.  *    None.
  355.  *----------------------------------------------------------------------
  356.  */
  357.  
  358. static void
  359. WaitForResponseBlock(ctrlPtr,crbPtr)
  360.     Controller    *ctrlPtr;         /* Controller to which command 
  361.                    * was submitted. */
  362.     volatile JaguarCRB *crbPtr;   /* Command Response Block to be filled in by
  363.                    * interrupt handler.    */
  364. {
  365.     while (!(crbPtr->status & JAGUAR_CRB_BLOCK_VALID)) {
  366.     Sync_MasterWait(&(ctrlPtr->ctrlCmdWait), &ctrlPtr->mutex,FALSE);
  367.     }
  368.     return;
  369. }
  370.  
  371.  
  372. /*
  373.  *----------------------------------------------------------------------
  374.  *
  375.  * InitializeWorkq --
  376.  *
  377.  *      Send Jaguar the command to initialize the specified work queue
  378.  *    with the given parameters and options. 
  379.  *
  380.  *     NOTE: This routine assumes that the error recovery method used 
  381.  *          in the driver would be to Freeze the Work queue upon error.
  382.  *
  383.  * Results:
  384.  *    TRUE is queue was initialized, FALSE otherwise.
  385.  *
  386.  * Side effects:
  387.  *    A work queue initialize command is send to the controller.
  388.  *
  389.  *----------------------------------------------------------------------
  390.  */
  391. /*ARGSUSED*/
  392. static Boolean
  393. InitializeWorkq(ctrlPtr,workqNum, parity, priority)
  394.     Controller    *ctrlPtr;    /* Controller to initalize Work Queue on. */
  395.     int        workqNum;    /* Workq to initialize must be 1-14. */
  396.     Boolean    parity;        /* TRUE - parity check should be enabled. When
  397.                  * communicated thru this workq. FALSE if not. 
  398.                  */
  399.     int        priority;    /* The priority level of this workq. (1-14) */
  400. {
  401.     JaguarIOPB        inMemIOPB;
  402.     volatile register JaguarIOPB    *iopb = &inMemIOPB;
  403.     JaguarCRB        crb;
  404.     Boolean        good;
  405.  
  406.     if (devJaguarDebug > 3) {
  407.     printf("%s: Initializing workQueue %d ...\n", ctrlPtr->name, workqNum);
  408.     }
  409.  
  410.     /*
  411.      * Build the appropriate Initialized Work Queue Command IOPB
  412.      * and send it to workQ 0. We must use workq 0 for this comand.
  413.      */
  414.     bzero((char *) &crb, sizeof(crb));
  415.     bzero((char *) iopb, sizeof(*iopb));
  416.     iopb->command = JAGUAR_INIT_WORK_QUEUE_CMD;
  417.     iopb->options = JAGUAR_IOPB_INTR_ENA;
  418.     iopb->intrVector = ctrlPtr->intrVector;
  419.     iopb->intrLevel = ctrlPtr->intrLevel;
  420.     iopb->cmd.workQueueArg.number = workqNum;
  421.     iopb->cmd.workQueueArg.options =
  422.              JAGUAR_WQ_INIT_QUEUE | 
  423.              JAGUAR_WQ_FREEZE_QUEUE | 
  424. #ifdef youWantItNotToWork
  425.     /*
  426.      * Setting the JAGUAR_WQ_PARITY_ENABLE bit in the workQueue options
  427.      * causes the HBA to quit working.
  428.      */
  429.              (parity ? JAGUAR_WQ_PARITY_ENABLE : 0);
  430. #else
  431.             0;
  432. #endif
  433.  
  434.     iopb->cmd.workQueueArg.slots = NUM_CQE;
  435.     iopb->cmd.workQueueArg.priority = priority;
  436.     /*
  437.      * Send the command into work queue zero. SendJaguarCmd will do the
  438.      * waiting for us and return 
  439.      */
  440.     MASTER_LOCK(&ctrlPtr->mutex);
  441.     good = SendJaguarCmd(ctrlPtr, 0, iopb, FILL_IN_CRB_ACTION,(ClientData)&crb);
  442.     MASTER_UNLOCK(&ctrlPtr->mutex);
  443.     if (!good) {
  444.     panic("%s: Initialize WorkQ %d did not finished.\n",ctrlPtr->name,
  445.            workqNum);
  446.     return (FALSE);
  447.     }
  448.     if (crb.status & (JAGUAR_CRB_ERROR|JAGUAR_CRB_EXCEPTION)) {
  449.     printf("%s: Initialize WorkQ %d failed with error 0x%s %s\n",
  450.          ctrlPtr->name, workqNum, crb.iopb.returnStatus, 
  451.          ErrorString(crb.iopb.returnStatus));
  452.     return (FALSE);
  453.  
  454.     }
  455.     if (devJaguarDebug > 3) {
  456.     printf("%s: WorkQueue %d initialized\n", ctrlPtr->name, workqNum);
  457.     }
  458.     return (TRUE);
  459. }
  460.  
  461. /*
  462.  *----------------------------------------------------------------------
  463.  *
  464.  * PerformDiagnostics --
  465.  *
  466.  *    Perform an extensive diagnostics test of the JaguarHBA.
  467.  *
  468.  *
  469.  * Results:
  470.  *    TRUE if the controller passes the test. FALSE if controller is broken.
  471.  *
  472.  * Side effects:
  473.  *    The docuementation says the JAGUAR_DIAG_CMD can take several
  474.  *    minutes to execute. I let it run for 45 mins and it still didn't
  475.  *    finish.  
  476.  *
  477.  *----------------------------------------------------------------------
  478.  */
  479.  
  480. static Boolean
  481. PerformDiagnostics(memPtr, name)
  482.     volatile JaguarMem *memPtr;    /* Pointer to VME Short IO space of HBA. */
  483.     char    *name;        /* Name of the controller for error messges. */
  484. {
  485.      register volatile JaguarCQE  *cqe;
  486.      register volatile JaguarIOPB *iopb;
  487.      register volatile JaguarCRB  *crb;
  488.      Boolean good = TRUE;
  489.  
  490.     cqe  = &(memPtr->mce);
  491.     iopb = &(memPtr->masterIOPB);
  492.     crb = &(memPtr->crb);
  493.     /*
  494.      * The command we send is the Perform Diagnostics command.
  495.      */
  496.     ZeroJaguarMem((short *)iopb, sizeof(iopb));
  497.     iopb->command = JAGUAR_DIAG_CMD;
  498.     {
  499.     register unsigned int status;
  500.     /*
  501.      * Send the command off and wait for response.
  502.      */
  503.     cqe->controlReg = JAGUAR_CQE_GO_BUSY;
  504.     if (!WaitForBitSet(&(crb->status),JAGUAR_CRB_BLOCK_VALID,100000)) {
  505.          panic("%s diag timeout. status = 0x%x\n", name, 
  506.            crb->status);
  507.          return FALSE;
  508.     }
  509.     /*
  510.      * Check for happy completion status.
  511.      */
  512.     status = crb->status;
  513.     if (!(status & JAGUAR_CRB_COMMAND_COMPLETE)) {
  514.          panic("%s diag cmd didn't complete, status 0x%x\n",
  515.             name, status);
  516.          return FALSE;
  517.     }
  518.     if (status & (JAGUAR_CRB_ERROR|JAGUAR_CRB_EXCEPTION)) {
  519.          printf("%s diag cmd error 0x%x, status 0x%x\n",
  520.             name, crb->iopb.returnStatus, status);
  521.         good = FALSE;
  522.     }
  523.     if (crb->iopb.cmd.diagArg.romTest != 0xffff) {
  524.         printf("%s failed ROM test, result = 0x%x\n", name ,
  525.                 crb->iopb.cmd.diagArg.romTest);
  526.         good = FALSE;
  527.     }
  528.     if (crb->iopb.cmd.diagArg.scrRamTest != 0xffff) {
  529.         printf("%s failed Scratch pad RAM test, result = 0x%x\n", name ,
  530.                 crb->iopb.cmd.diagArg.scrRamTest);
  531.         good = FALSE;
  532.     }
  533.     if (crb->iopb.cmd.diagArg.bufRamTest != 0xffff) {
  534.         printf("%s failed Buffer RAM test, result = 0x%x\n", name ,
  535.                 crb->iopb.cmd.diagArg.bufRamTest);
  536.         good = FALSE;
  537.     }
  538.     if (crb->iopb.cmd.diagArg.eventRamTest != 0xffff) {
  539.         printf("%s failed Evant RAM test, result = 0x%x\n", name ,
  540.                 crb->iopb.cmd.diagArg.eventRamTest);
  541.         good = FALSE;
  542.     }
  543.     if (crb->iopb.cmd.diagArg.priPort != 0xffff) {
  544.         printf("%s failed Primary SCSI port test, result = 0x%x\n", name ,
  545.                 crb->iopb.cmd.diagArg.priPort);
  546.         good = FALSE;
  547.     }
  548.     if (crb->iopb.cmd.diagArg.secPort != 0xffff) {
  549.         printf("%s failed Secondary SCSI port test, result = 0x%x\n", name ,
  550.                 crb->iopb.cmd.diagArg.secPort);
  551.         good = FALSE;
  552.     }
  553.     /*
  554.      * If we got here the controller init cmd successfully completed.
  555.      * Acknowledge the command to release the CRB.
  556.      */
  557.     crb->status = 0;
  558.     }
  559.     return good;
  560. }
  561.  
  562.  
  563. /*
  564.  *----------------------------------------------------------------------
  565.  *
  566.  * InitializeJaguar --
  567.  *
  568.  *    Initialize the Jaguar HBA and ready it for commands.
  569.  *
  570.  *    This routine assumes that the controller is MASTER_LOCKed()
  571.  *
  572.  * Results:
  573.  *    TRUE if the controller initializes. FALSE if controller is broken.
  574.  *
  575.  * Side effects:
  576.  *    Controller is reset and initialized. This causes the SCSI bus(es) to
  577.  *    be reset and any in progress commands aborted. 
  578.  *
  579.  *----------------------------------------------------------------------
  580.  */
  581.  
  582. static Boolean
  583. InitializeJaguar(memPtr, name, intrLevel, intrVector)
  584.     volatile JaguarMem *memPtr;    /* Pointer to VME Short IO space of HBA. */
  585.     char    *name;        /* Name of the controller for error messges. */
  586.     int        intrLevel;    /* VME Interrupt level for HBA. */
  587.     int        intrVector;    /* VME Interrupt vector for HBA. */
  588. {
  589.      register volatile JaguarMCSB *mcsb = &(memPtr->mcsb);
  590.      register volatile JaguarCQE  *cqe;
  591.      register volatile JaguarIOPB *iopb;
  592.      register volatile JaguarCRB  *crb;
  593.  
  594.     /*
  595.      * Start off with a clean slate by reseting the board. Documentation
  596.      * says must keep reset bit set at least 50 microseconds.
  597.      * We give it 1 millesecond of reset .
  598.      */
  599.  
  600.     mcsb->control = JAGUAR_MCR_RESET;
  601.     MACH_DELAY(1000); 
  602.     mcsb->control = 0;
  603.     /*
  604.      * Wait for the Jaugar to signal Board OK. Board OK not valid for
  605.      * 100 microseconds after reset. We give it 1 millisecond to be happy.
  606.      *
  607.      * One millisecond does not appear to be long enough because the
  608.      * OK bit is still not set. We increase the amount to 1 second to 
  609.      * make sure.
  610.      */
  611.     MACH_DELAY(1000*1000);
  612.     if (!(mcsb->status & JAGUAR_MSR_BOARD_OK)) {
  613.     panic("Warning: %s board not OK, status = 0x%x\n", name, mcsb->status);
  614.     return (FALSE);
  615.     }
  616.  
  617.  
  618.     /*
  619.      * Initialize the rest of the MCSB. Currently we wont deal with 
  620.      * queue available interrupts so we turn them off.
  621.      */
  622.     mcsb->queueAvail = 0;
  623.     mcsb->queueHead = 0;
  624.     mcsb->reserved[0] = mcsb->reserved[1] = mcsb->reserved[2] = 0;
  625.     /*
  626.      * Initialize the Command Queue (CQ). To do this we clear out the 
  627.      * CQE's and point them at their IOPBs. Clear out the IOPBs too.
  628.      */
  629.      {
  630.      int    i;
  631.      cqe  = memPtr->cmdQueue;
  632.      iopb = memPtr->iopbs;
  633.      for (i = 0; i < NUM_CQE; i++, cqe++, iopb++) {
  634.          cqe->controlReg = 0;
  635.          cqe->iopbOffset = POINTER_TO_OFFSET(iopb,memPtr);
  636.          cqe->iopbLength = sizeof(JaguarIOPB)/4;
  637.          cqe->commandTag[0] = 0;
  638.          cqe->commandTag[1] = 0;
  639.          cqe->workQueue = 0;
  640.          cqe->reserved = 0;
  641.      }
  642.      ZeroJaguarMem((short *) iopb, sizeof(JaguarIOPB) * NUM_CQE);
  643.    }
  644.     /*
  645.      * Clear the area to be used as the Command Response Block.
  646.      */
  647.     crb = &(memPtr->crb);
  648.     ZeroJaguarMem((short *) crb, sizeof(JaguarCRB));
  649.  
  650.     /*
  651.      * Initialize the Master Control Enter (MCE) so we can send commands
  652.      * to the board. 
  653.      */
  654.     cqe  = &(memPtr->mce);
  655.     iopb = &(memPtr->masterIOPB);
  656.     cqe->controlReg = 0;
  657.     cqe->iopbOffset = POINTER_TO_OFFSET(iopb,memPtr);
  658.     cqe->iopbLength = sizeof(JaguarIOPB)/4;
  659.     cqe->workQueue = 0;
  660.     cqe->reserved = 0;
  661.     cqe->commandTag[0] = 0;
  662.     cqe->commandTag[1] = 0;
  663.     ZeroJaguarMem((short *)iopb , sizeof(JaguarIOPB));
  664.  
  665.     if (devJaguarDebug > 9) {
  666.     if (!PerformDiagnostics(memPtr, name)) {
  667.         printf("Warning: %s failed diagnostics\n");
  668.     }
  669.     }
  670.     /*
  671.      * The first command we send is the Initialize controller command.
  672.      * In order to send this command we must build a Controller
  673.      * Initialization Block (CIP) in JaguarMem.  Since this block is 
  674.      * not needed after initialization, we overlay the scatter sgElements
  675.      * gather vectors with the CIP.
  676.      */
  677.     {
  678.     volatile JaguarCIB *cib = (volatile JaguarCIB *) (memPtr->sgElements);
  679.     ZeroJaguarMem((short *) cib, sizeof(JaguarCIB));
  680.  
  681.     cib->numQueueSlots = NUM_CQE;
  682.     cib->dmaBurstCount = DMA_BURST_COUNT;
  683.     cib->normalIntrVector =    JAGUAR_INTR_VECTOR(intrLevel,intrVector);
  684.     cib->errorIntrVector = JAGUAR_INTR_VECTOR(intrLevel,intrVector);
  685.     cib->priTargetID = JAGUAR_DEFAULT_BUS_ID;
  686.     cib->secTargetID = JAGUAR_DEFAULT_BUS_ID;
  687.     cib->offsetCRB = POINTER_TO_OFFSET(&(memPtr->crb),memPtr);
  688.     SET_LONG(cib->scsiSelTimeout, SELECTION_TIMEOUT);
  689.     SET_LONG(cib->scsiReselTimeout, RESELECTION_TIMEOUT);
  690.     SET_LONG(cib->vmeTimeout, VME_TIMEOUT);
  691.  
  692.     ZeroJaguarMem((short *)iopb, sizeof(iopb));
  693.     iopb->command = JAGUAR_INIT_HBA_CMD;
  694.     iopb->addrModifier = JAGUAR_BOARD_MEM_TYPE;
  695.     SET_LONG(iopb->bufferAddr,POINTER_TO_OFFSET(cib,memPtr));
  696.     }
  697.     {
  698.     register unsigned int status;
  699.     /*
  700.      * Send the command off and wait for response.
  701.      */
  702.     cqe->controlReg = JAGUAR_CQE_GO_BUSY;
  703.     if (!WaitForBitSet(&(crb->status),JAGUAR_CRB_BLOCK_VALID,10000)) {
  704.          panic("%s init controller timeout. status = 0x%x\n", name, 
  705.            crb->status);
  706.          return FALSE;
  707.     }
  708.     /*
  709.      * Check for happy completion status.
  710.      */
  711.     status = crb->status;
  712.     if (!(status & JAGUAR_CRB_COMMAND_COMPLETE)) {
  713.          panic("%s init ctrl cmd didn't complete, status 0x%x\n",
  714.             name, status);
  715.          return FALSE;
  716.     }
  717.     if (status & (JAGUAR_CRB_ERROR|JAGUAR_CRB_EXCEPTION)) {
  718.          panic("%s init ctrl cmd error 0x%x, status 0x%x\n",
  719.             name, iopb->returnStatus, status);
  720.          return FALSE;
  721.     }
  722.     /*
  723.      * If we got here the controller init cmd successfully completed.
  724.      * Acknowledge the command to release the CRB.
  725.      */
  726.     crb->status = 0;
  727.     /*
  728.      * Start Queue Mode operation and wait for ack.
  729.      */
  730.     mcsb->control |= JAGUAR_MCR_START_QUEUES;
  731.     if (!WaitForBitSet(&(crb->status),JAGUAR_CRB_BLOCK_VALID,10000)) {
  732.          panic("%s start queue mode timeout.\n",name);
  733.          return FALSE;
  734.     }
  735.     status = crb->status;
  736.     if (!(status & JAGUAR_CRB_QUEUE_START)) {
  737.          panic("%s start queue mode bad status 0x%x.\n",name,status);
  738.          return FALSE;
  739.     }
  740.     crb->status = 0;
  741.     }
  742.     /*
  743.      * Be neat! Clear out the rest of the Jaguar memory.
  744.      */
  745.     ZeroJaguarMem((short *) (memPtr->sgElements), 
  746.              NUM_SG_ELEMENTS * sizeof(JaguarSG));
  747.     ZeroJaguarMem((short *) (memPtr->padding), MEM_PAD);
  748.     /*
  749.      * Notifiy the world that we are alive by printing our Controller
  750.      * Configuration Status Block. This will be useful if someone ask
  751.      * the question: "What rev PROMs are you running?"
  752.      */
  753.     { 
  754.     JaguarCCSB css;
  755.     /*
  756.      * Note we copy the CCSB from the board because passing pointers
  757.      * into Jaguar  Memory to printf would be a no-no because we 
  758.      * don't have control of the type memory references done by
  759.      * printf.
  760.      */
  761.     CopyFromJaguarMem((short *)&(memPtr->css),(short *)&css, sizeof(css));
  762.     printf("%s firmware (%3s-%c-%3s) %c%c/%c%c/%c%c ",
  763.         name, css.code, css.variation, css.firmwareLevel,
  764.         css.firmwareDate[0], css.firmwareDate[1],
  765.         css.firmwareDate[2], css.firmwareDate[3], 
  766.         css.firmwareDate[6], css.firmwareDate[7]);
  767.     printf("%dK RAM bus0ID %d bus1ID %d\n", css.bufferRAMsize,
  768.         css.primaryID, css.secondaryID);
  769.     }
  770.     return TRUE;
  771. }
  772.  
  773. static struct errorString {
  774.     int    code;
  775.     char *string ;
  776. } errorStrings[] = JAGUAR_ERROR_CODES;
  777. #define NUM_ERROR_STRINGS (sizeof(errorStrings) / sizeof(errorStrings[0]))
  778.  
  779.  
  780. /*
  781.  *----------------------------------------------------------------------
  782.  *
  783.  * ErrorString --
  784.  *
  785.  *    Return a string describing a Jaguar error code.
  786.  *
  787.  * Results:
  788.  *    An error string.
  789.  *
  790.  * Side effects:
  791.  *    None.
  792.  *
  793.  *----------------------------------------------------------------------
  794.  */
  795.  
  796. static char *
  797. ErrorString(returnStatus)
  798.     unsigned short    returnStatus;
  799. {
  800.     int    code = returnStatus & 0xff;
  801.     int    i;
  802.  
  803.     for (i = 0; i < NUM_ERROR_STRINGS; i++) {
  804.     if (errorStrings[i].code == code) {
  805.         return errorStrings[i].string;
  806.     }
  807.     }
  808.     return "Unknown error";
  809. }
  810.  
  811. /*
  812.  *----------------------------------------------------------------------
  813.  *
  814.  * MapJaguarToSpriteErrorCode --
  815.  *
  816.  *    Map a jaguar return error code to a Sprite ReturnStatus value.
  817.  *
  818.  * Results:
  819.  *    A sprite return status value.
  820.  *
  821.  * Side effects:
  822.  *    None.
  823.  *
  824.  *----------------------------------------------------------------------
  825.  */
  826.  
  827. static ReturnStatus 
  828. MapJaguarToSpriteErrorCode(status)
  829.     unsigned short    status;
  830. {
  831.     int    code = status & 0xff;
  832.  
  833.     if (code == 0) {
  834.     return SUCCESS;
  835.     }
  836.     /*
  837.      * Error codes between 0x20 and 0x30 are VME bus type errors.
  838.      */
  839.     if (code >= 0x20 && code < 0x30) {
  840.     return DEV_DMA_FAULT;
  841.     }
  842.     return DEV_HARD_ERROR;
  843. }
  844.  
  845. /*
  846.  *----------------------------------------------------------------------
  847.  *
  848.  *  SendScsiCommand --
  849.  *
  850.  *    Enqueue a SCSI command into the Jaguar command queue.
  851.  *
  852.  * Results:
  853.  *    
  854.  *
  855.  * Side effects:
  856.  *
  857.  *----------------------------------------------------------------------
  858.  */
  859.  
  860. static Boolean
  861. SendScsiCommand( devPtr, scsiCmdPtr)
  862.     Device *devPtr; /* Jaguar device for command. */
  863.     ScsiCmd    *scsiCmdPtr; /* SCSI command to send. */
  864.  
  865. {
  866.     volatile JaguarIOPB iopbMem;    
  867.     Boolean    retVal;
  868.  
  869.     /*
  870.      * Check to see if we can use normal PASS_THRU command or must be
  871.      * PASS_THRU_EXT extented.
  872.      */
  873.     if (scsiCmdPtr->commandBlockLen <= sizeof(iopbMem.cmd.scsiArg.cmd)) {
  874.     /*
  875.      * We can use a simple PASS_THRU command. 
  876.      */
  877.     FillInScsiIOPB(devPtr, scsiCmdPtr, &iopbMem);
  878.     retVal = SendJaguarCmd(devPtr->ctrlPtr, devPtr->workQueue, &iopbMem, 
  879.                 SCSI_CMD_ACTION, (ClientData) scsiCmdPtr);
  880.     } else {
  881.     /*
  882.      * Can't handle PASS_THRU_EXT command yet.
  883.      */
  884.     panic("%s: SCSI command too large, %d bytes.\n",devPtr->ctrlPtr->name, 
  885.                     scsiCmdPtr->commandBlockLen);
  886.     return (FALSE);
  887.     }
  888.     return (retVal);
  889.  
  890. }
  891.  
  892. /*
  893.  *----------------------------------------------------------------------
  894.  *
  895.  * DevJaguarIntr --
  896.  *
  897.  *    Process a Jaguar interrupt by processing the entry in the ctrl's
  898.  *    Command Response Block and reseting the controller.
  899.  *
  900.  * Results:
  901.  *    TRUE if an interrupt was process. FALSE if not interrupt was
  902.  *    present on this controller.
  903.  *
  904.  * Side effects:
  905.  *    None.
  906.  *
  907.  *----------------------------------------------------------------------
  908.  */
  909.  
  910. Boolean
  911. DevJaguarIntr(clientData)
  912.     ClientData    clientData;    /* Controller to process. */
  913. {
  914.     register volatile JaguarCRB *crb;
  915.     unsigned int            status;
  916.     unsigned int             returnStatus;
  917.     CmdAction                *actionPtr;
  918.     register Controller         *ctrlPtr;
  919.  
  920.     ctrlPtr = (Controller *) clientData;
  921.     crb = &(ctrlPtr->memPtr->crb);
  922.     /*
  923.      * Check the controller's CRB. If BLOCK_VALID is we have some sort
  924.      * of condition present.
  925.      */
  926.     status = crb->status;
  927.     if (!(status & JAGUAR_CRB_BLOCK_VALID)) {
  928.     return (FALSE);
  929.     }
  930.     /*
  931.      * Classify the condition. If should be one of the following:
  932.      * 1) A command just completed - COMMAND_COMPLETE.
  933.      * 2) Queue mode was started - QUEUE_START
  934.      * 3) A command queue entry became available. - QUEUE_AVAILABLE
  935.      * 
  936.      * QUEUE_START interrupts are uninteresting to us. No processing
  937.      * is necessary.  QUEUE_AVAILABLE are also uninteresting and 
  938.      * unless I don't understand something impossible because we
  939.      * never set anything in the IQAR on the MCSB. Because these
  940.      * interrupt are boring we processing them first.
  941.      */
  942.     if (status & (JAGUAR_CRB_QUEUE_START|JAGUAR_CRB_QUEUE_AVAILABLE)){
  943.     /*
  944.      * Clear the interrupt.
  945.      */
  946.     crb->status = 0;
  947.     return (TRUE);
  948.     }
  949.     if (!(status & JAGUAR_CRB_COMMAND_COMPLETE)) {
  950.     printf("Warning: %s unknown interrupt, status = 0x%x\n", 
  951.         ctrlPtr->name, status);
  952.     crb->status = 0;
  953.     return (TRUE);
  954.     }
  955.     /*
  956.      * Since all work queues should be setup with FREEZE on error and not
  957.      * ABORT on error, take aborted commands very seriously.
  958.      */
  959.     if (status & JAGUAR_CRB_ABORTED) {
  960.     panic("%s command aborted, status = 0x%x!!!\n", 
  961.             ctrlPtr->name,status);
  962.     crb->status = 0;
  963.     return (TRUE);
  964.     }
  965.     /*
  966.      * We have a real COMMAND_COMPLETION. Read the tag out of the
  967.      * return CQE to find the action to be performed. Also, read the
  968.      * return status from the IOPB returned.
  969.      */
  970.     actionPtr = &(ctrlPtr->cmdAction[crb->commandTag[0]]);
  971.     /*
  972.      * Release the device's DMA space.
  973.      */
  974.     if (actionPtr->dmaBufferLen > 0) {
  975.     if (((unsigned)actionPtr->dmaBuffer) & 0x80000000) { 
  976.         VmMach_32BitDMAFree(actionPtr->dmaBufferLen, actionPtr->dmaBuffer);
  977.     } else {
  978.         VmMach_DMAFree(actionPtr->dmaBufferLen, actionPtr->dmaBuffer);
  979.     }
  980.     }
  981.     returnStatus = crb->iopb.returnStatus;
  982.     /*
  983.      * Check to see an error.
  984.      */
  985.     if (actionPtr->action & FILL_IN_CRB_ACTION) {
  986.     CopyFromJaguarMem((short *)crb,(short *) actionPtr->actionArg, 
  987.                 sizeof(*crb));
  988.     } 
  989.     if (actionPtr->action & SCSI_CMD_ACTION) {
  990.     ScsiCmd    *scsiCmdPtr = (ScsiCmd *) actionPtr->actionArg;
  991.     Device    *devPtr = (Device *) (ctrlPtr->devices[crb->workQueue-1]);
  992.     ReturnStatus    spriteStatus = SUCCESS;
  993.     int    transferCount = READ_LONG(crb->iopb.maxXferLen);
  994.     if (returnStatus & 0xff) {
  995.         /*
  996.          * Error code 0x34 means that the transfer count didn't match
  997.          * the number of bytes transfers. We don't consider this an 
  998.          * error.
  999.          */
  1000.         if ((returnStatus & 0xff) != 0x34)  {
  1001.         spriteStatus = MapJaguarToSpriteErrorCode(returnStatus);
  1002.         transferCount = 0;
  1003.         } 
  1004.         if (spriteStatus != SUCCESS) {
  1005.         printf("Warning: Device %s HBA error 0x%x: %s\n",
  1006.             devPtr->handle.locationName, returnStatus,
  1007.             ErrorString(returnStatus));
  1008.         }
  1009.     }  
  1010. #ifdef ONE_CMD_AT_A_TIME
  1011.     ctrlPtr->numActiveCmds--;
  1012. #else
  1013.     devPtr->numActiveCmds--;
  1014. #endif /* ONE_CMD_AT_A_TIME */
  1015.     RequestDone(devPtr, scsiCmdPtr, spriteStatus,
  1016.             (returnStatus >> 8) & 0xff, transferCount);
  1017.     StartNextRequest(devPtr);
  1018.  
  1019.     }
  1020.     if (IS_WAIT_ACTION(actionPtr->action)) {
  1021.     Sync_MasterBroadcast(&(ctrlPtr->ctrlCmdWait));
  1022.     }
  1023.     actionPtr->action = UNUSED_ACTION;
  1024.     crb->status = 0;
  1025.     return (TRUE);
  1026.  
  1027. }
  1028.  
  1029.  
  1030. /*
  1031.  *----------------------------------------------------------------------
  1032.  *
  1033.  *  SendJaguarCmd --
  1034.  *
  1035.  *    Enter a Jaguar command into the specified Jaguar work Queue.
  1036.  *
  1037.  * Results:
  1038.  *    TRUE if the command was started. FALSE otherwise.
  1039.  *
  1040.  * Side effects:
  1041.  *    Those of Jaguar commands.
  1042.  *
  1043.  *----------------------------------------------------------------------
  1044.  */
  1045.  
  1046. static Boolean
  1047. SendJaguarCmd(ctrlPtr, workQueue, iopbPtr, action, actionArg)
  1048.     Controller    *ctrlPtr;    /* Controller to enter command. */
  1049.     int        workQueue;    /* Command destination workq number. */
  1050.     volatile JaguarIOPB *iopbPtr;    /* Jaguar IOPB command block. */
  1051.     int        action;        /* Action to be performed on completion. */
  1052.     ClientData  actionArg;    /* Argument to action. */
  1053. {
  1054.     volatile JaguarMem    *memPtr = ctrlPtr->memPtr;
  1055.     volatile JaguarCQE    *cqe;
  1056.     volatile JaguarIOPB    *iopb;
  1057.     Boolean    noDMA = FALSE;
  1058.     unsigned int  addr;
  1059.  
  1060.     /*
  1061.      * Work queue is special because it has only one entry. To keep from
  1062.      * overrunning it we must observe a locking protocol.
  1063.      */ 
  1064.     if (workQueue == 0) {
  1065.     LockWorkq0(ctrlPtr);    
  1066.     }
  1067.     /*
  1068.      * Get the next Command Queue entry stepping the next available queue
  1069.      * entry pointer around the circular queue.
  1070.      */
  1071.     cqe = ctrlPtr->nextCQE++;
  1072.     if ((ctrlPtr->nextCQE - memPtr->cmdQueue) >= NUM_CQE) {
  1073.     ctrlPtr->nextCQE = memPtr->cmdQueue;
  1074.     }
  1075.     if (cqe->controlReg & JAGUAR_CQE_GO_BUSY) {
  1076.     panic("%s: Command Queue Full\n", ctrlPtr->name);
  1077.     }
  1078.     addr = (unsigned) READ_LONG(iopbPtr->bufferAddr);
  1079.     {
  1080.     /*
  1081.      * If the address has the lowest hit set then it was already
  1082.      * pointing into the DMA space so we set the dmaBufferLen
  1083.      * to zero to cause the interrupt handle not to free it.
  1084.      */
  1085.     if (addr & 1) { 
  1086.         addr = addr & ~1;
  1087.         SET_LONG(iopbPtr->bufferAddr,addr);
  1088.         noDMA = TRUE;
  1089.     }
  1090.     }
  1091.     /*
  1092.      * Find to IOPB we hardwired this CQE to point at.   
  1093.      */
  1094.     iopb = memPtr->iopbs + (cqe - memPtr->cmdQueue);
  1095.     /*
  1096.      * Fill in the on-board iopb with the one our caller passed us.
  1097.      */
  1098.     CopyToJaguarMem((short *) iopbPtr, (short *)iopb, sizeof(*iopb));
  1099.     cqe->workQueue = workQueue;
  1100.      /*
  1101.      * Inform interrupt handler of the action we want on completion.
  1102.      * To do this we must allocate a cmdAction buffer. We store the 
  1103.      * index into cmdAction in the cqe's commandTag to allow the 
  1104.      * interrupt handler to associated it with the command.
  1105.      */
  1106.     {
  1107.     CmdAction *actionPtr;
  1108.     while (ctrlPtr->cmdAction[ctrlPtr->nextActionBuffer].action
  1109.                     != UNUSED_ACTION) {
  1110.         ctrlPtr->nextActionBuffer++;
  1111.         if (ctrlPtr->nextActionBuffer >= NUM_ACTIONS) {
  1112.         ctrlPtr->nextActionBuffer = 0;
  1113.         }
  1114.     }
  1115.     actionPtr = &(ctrlPtr->cmdAction[ctrlPtr->nextActionBuffer]);
  1116.     actionPtr->action = action;
  1117.     /*
  1118.      * If the address has the lowest hit set then it was already
  1119.      * pointing into the DMA space so we set the dmaBufferLen
  1120.      * to zero to cause the interrupt handle not to free it.
  1121.      */
  1122.     if (noDMA) { 
  1123.         actionPtr->dmaBufferLen = 0;
  1124.     } else { 
  1125.         actionPtr->dmaBufferLen = READ_LONG(iopbPtr->maxXferLen);
  1126.     }
  1127.     if ((unsigned)addr & 0x80000000) {
  1128.         actionPtr->dmaBuffer = (Address) (addr);
  1129.     } else {
  1130.         actionPtr->dmaBuffer = (Address) (addr + VMMACH_DMA_START_ADDR);
  1131.     }
  1132.     actionPtr->actionArg = actionArg;
  1133.         cqe->commandTag[0] = ctrlPtr->nextActionBuffer;
  1134.     }
  1135.     /*
  1136.      * Inform the controller that the command is ready.
  1137.      */
  1138.     cqe->controlReg = JAGUAR_CQE_GO_BUSY;
  1139.     /*
  1140.      * If the caller specified a CRB we wait for the response.
  1141.      */
  1142.     if(IS_WAIT_ACTION(action)) { 
  1143.     WaitForResponseBlock(ctrlPtr,(volatile JaguarCRB *) actionArg);
  1144.     }
  1145.     if (workQueue == 0) {
  1146.     UnLockWorkq0(ctrlPtr);    
  1147.     }
  1148.     return (TRUE);
  1149. }
  1150.  
  1151. /*
  1152.  *----------------------------------------------------------------------
  1153.  *
  1154.  * LockWorkq0 --
  1155.  *
  1156.  *    Grap exclusive access to work queue 0 of the specified controller.
  1157.  *
  1158.  * Results:
  1159.  *    None.
  1160.  *
  1161.  * Side effects:
  1162.  *    None.
  1163.  *
  1164.  *----------------------------------------------------------------------
  1165.  */
  1166.  
  1167. static void
  1168. LockWorkq0(ctrlPtr)
  1169.     Controller    *ctrlPtr;
  1170. {
  1171.     while (ctrlPtr->workQueue0Busy) {
  1172.     Sync_MasterWait(&(ctrlPtr->ctrlQueue0Wait), &ctrlPtr->mutex,FALSE);
  1173.     }
  1174.     ctrlPtr->workQueue0Busy = TRUE;
  1175. }
  1176.  
  1177. /*
  1178.  *----------------------------------------------------------------------
  1179.  *
  1180.  * UnLockWorkq0 --
  1181.  *
  1182.  *    Release exclusive access to work queue 0 of the specified controller.
  1183.  *
  1184.  * Results:
  1185.  *    None.
  1186.  *
  1187.  * Side effects:
  1188.  *    None.
  1189.  *
  1190.  *----------------------------------------------------------------------
  1191.  */
  1192.  
  1193. static void
  1194. UnLockWorkq0(ctrlPtr)
  1195.     Controller    *ctrlPtr;
  1196. {
  1197.     ctrlPtr->workQueue0Busy = FALSE;
  1198.     Sync_MasterBroadcast(&(ctrlPtr->ctrlQueue0Wait));
  1199. }
  1200.  
  1201.  
  1202. /*
  1203.  *----------------------------------------------------------------------
  1204.  *
  1205.  * CopyFromJaguarMem --
  1206.  *
  1207.  *    Copy a block from Jaguar Memory to host memory.
  1208.  *     NOTE:
  1209.  *        This routine assumes that both the block and the host
  1210.  *        memory region are aligned on a 2 byte boundry and are
  1211.  *        an even number of bytes long.
  1212.  *
  1213.  * Results:
  1214.  *    None.
  1215.  *
  1216.  * Side effects:
  1217.  *    None.
  1218.  *
  1219.  *----------------------------------------------------------------------
  1220.  */
  1221.  
  1222. static void
  1223. CopyFromJaguarMem(blockPtr, hostPtr, blockSize)
  1224.     register short    *blockPtr;    /* Block in Jaguar Memory to copy. */
  1225.     register short    *hostPtr;    /* Address in host memory. */
  1226.     register int    blockSize;    /* Size of block in bytes. */
  1227.  
  1228. {
  1229.     /*
  1230.      * We do the copy 2 bytes at a time because the Jaguar is in 16 bit
  1231.      * data IO space of the VME bus.
  1232.      */
  1233.     while (blockSize > 0) {
  1234.     *(hostPtr++) = *(blockPtr++);
  1235.     blockSize -= sizeof(short);
  1236.     }
  1237. }
  1238.  
  1239.  
  1240. /*
  1241.  *----------------------------------------------------------------------
  1242.  *
  1243.  * CopyToJaguarMem --
  1244.  *
  1245.  *    Copy a block to Jaguar Memory from host memory.
  1246.  *     NOTE:
  1247.  *        This routine assumes that both the block and the host
  1248.  *        memory region are aligned on a 2 byte boundry and are
  1249.  *        an even number of bytes long.
  1250.  *
  1251.  * Results:
  1252.  *    None.
  1253.  *
  1254.  * Side effects:
  1255.  *    None.
  1256.  *
  1257.  *----------------------------------------------------------------------
  1258.  */
  1259.  
  1260. static void
  1261. CopyToJaguarMem(blockPtr, hostPtr, blockSize)
  1262.     register short    *blockPtr;    /* Block in Jaguar Memory to copy. */
  1263.     register short    *hostPtr;    /* Address in host memory. */
  1264.     register int    blockSize;    /* Size of block in bytes. */
  1265.  
  1266. {
  1267.     /*
  1268.      * We do the copy 2 bytes at a time because the Jaguar is in 16 bit
  1269.      * data IO space of the VME bus.
  1270.      */
  1271.     while (blockSize > 0) {
  1272.     *(hostPtr++) = *(blockPtr++);
  1273.     blockSize -= sizeof(short);
  1274.     }
  1275. }
  1276.  
  1277.  
  1278. /*
  1279.  *----------------------------------------------------------------------
  1280.  *
  1281.  * ZeroJaguarMem --
  1282.  *
  1283.  *    Zero a block of Jaguar board memory. 
  1284.  *
  1285.  * NOTE: This routine assumes the block is aligned on a 2 byte boundry and
  1286.  *     is an even number of bytes long.
  1287.  *
  1288.  * Results:
  1289.  *    None.
  1290.  *
  1291.  * Side effects:
  1292.  *    None.
  1293.  *
  1294.  *----------------------------------------------------------------------
  1295.  */
  1296.  
  1297. static void
  1298. ZeroJaguarMem(blockPtr, blockSize)
  1299.     register short    *blockPtr;    /* Pointer to block to zero. */
  1300.     register int    blockSize;    /* Number of bytes in the block. */
  1301. {
  1302.     /*
  1303.      * We do the zeroing 2 bytes at a time because the Jaguar is in 16 bit
  1304.      * data IO space of the VME bus.
  1305.      */
  1306.     while (blockSize > 0) {
  1307.     *(blockPtr++) = 0;
  1308.     blockSize -= sizeof(short);
  1309.     }
  1310. }
  1311.  
  1312.  
  1313. /*
  1314.  *----------------------------------------------------------------------
  1315.  *
  1316.  *  WaitForBitSet --
  1317.  *
  1318.  *    Wait for a bit to become set in a Jaguar word.
  1319.  *
  1320.  *
  1321.  * Results:
  1322.  *    TRUE if the bit appears. FALSE if we timeout.
  1323.  *
  1324.  * Side effects:
  1325.  *    None.
  1326.  *
  1327.  *----------------------------------------------------------------------
  1328.  */
  1329.  
  1330. static Boolean
  1331. WaitForBitSet(wordPtr, bit, maxCount)
  1332.     register volatile unsigned short *wordPtr;    /* Word to check. */
  1333.     register unsigned short bit;    /* Bit to check for. */
  1334.     int         maxCount;            /* Number of 100 microseconds to check 
  1335.                      * before giving up. */
  1336. {
  1337.     /*
  1338.      * Timeout after waiting one second, poll every 100 microseconds.
  1339.      */
  1340.     for(; maxCount > 0; maxCount--) {
  1341.     if (*wordPtr & bit) {
  1342.         return (TRUE);
  1343.     }
  1344.     MACH_DELAY(100);
  1345.     }
  1346.     return ((*wordPtr & bit) != 0);
  1347. }
  1348.  
  1349.  
  1350. /*
  1351.  *----------------------------------------------------------------------
  1352.  *
  1353.  * CheckSizes --
  1354.  *
  1355.  *    Check the sizes of the Jaguar structure declarations to insure they
  1356.  *    are consistent with the controller's ideas.  This routine is 
  1357.  *    intended to catch padding introduced by the compiler that may 
  1358.  *    break the driver's code. This checking should really be done at
  1359.  *    compile time but run time checking is better nothing. 
  1360.  *
  1361.  * Results:
  1362.  *    TRUE if the sizes checked are ok, FALSE otherwise.
  1363.  *
  1364.  * Side effects:
  1365.  *    None.
  1366.  *
  1367.  *----------------------------------------------------------------------
  1368.  */
  1369.  
  1370. static Boolean
  1371. CheckSizes()
  1372. {
  1373.     return (
  1374.         (SIZE_JAUGAR_MEM == sizeof(JaguarMem)) &&
  1375.     (JAGUAR_MCSB_SIZE == sizeof(JaguarMCSB)) &&
  1376.     (JAGUAR_CQE_SIZE == sizeof(JaguarCQE)) &&
  1377.     (JAGUAR_CCSB_SIZE == sizeof(JaguarCCSB)) &&
  1378.     (JAGUAR_CIB_SIZE == sizeof(JaguarCIB)) &&
  1379.     (JAGUAR_MAX_IOBP_SIZE == sizeof(JaguarIOPB)) &&
  1380.     (JAGUAR_CRB_SIZE == (sizeof(JaguarCRB) - sizeof(JaguarIOPB))) &&
  1381.     (JAGUAR_SG_SIZE == sizeof(JaguarSG))
  1382.     );
  1383. }
  1384.  
  1385. /*
  1386.  *----------------------------------------------------------------------
  1387.  *
  1388.  * FillInScsiIOPB --
  1389.  *
  1390.  *    Fill in a Jaguar IOPB with a SCSI PASS-THRU command to send 
  1391.  *    the specified scsi command block to the specified device.
  1392.  *
  1393.  * Results:
  1394.  *    None.
  1395.  *
  1396.  * Side effects:
  1397.  *    None.
  1398.  *
  1399.  *----------------------------------------------------------------------
  1400.  */
  1401.  
  1402. static void
  1403. FillInScsiIOPB(devPtr, scsiCmdPtr, iopbPtr)
  1404.     Device    *devPtr;    /* Target device for command. */
  1405.     ScsiCmd    *scsiCmdPtr;    /* SCSI command being sent. */
  1406.     volatile JaguarIOPB    *iopbPtr;    /* IOPB to be filled in . */
  1407. {
  1408.     Address    addr;
  1409.  
  1410.     bzero((char *)iopbPtr, sizeof(JaguarIOPB));
  1411.     iopbPtr->command = JAGUAR_PASS_THRU_CMD;
  1412.     iopbPtr->options = JAGUAR_IOPB_INTR_ENA | 
  1413.             (scsiCmdPtr->dataToDevice ? JAGUAR_IOPB_TO_HBA : 0);
  1414.     iopbPtr->intrVector =  devPtr->ctrlPtr->intrVector;
  1415.     iopbPtr->intrLevel = devPtr->ctrlPtr->intrLevel;
  1416.     iopbPtr->addrModifier = JAGUAR_ADDRESS_MODIFIER;
  1417.     if (scsiCmdPtr->bufferLen > 0) {
  1418.     /*
  1419.      * If the address is already in DMA space we do not have to
  1420.      * map it.  We set the lowest bit of the address to inform
  1421.      * the interrupt handler not to free it.
  1422.      */
  1423.     if (((unsigned) scsiCmdPtr->buffer) < (unsigned)VMMACH_DMA_START_ADDR) {
  1424.         addr = VmMach_32BitDMAAlloc(scsiCmdPtr->bufferLen, 
  1425.                      scsiCmdPtr->buffer);
  1426.     } else {
  1427.         addr = (Address) (((unsigned) scsiCmdPtr->buffer) | 1);
  1428.     }
  1429.     } else {
  1430.     addr = (Address) VMMACH_DMA_START_ADDR;
  1431.     }
  1432.     if ((unsigned) addr < (unsigned)VMMACH_DMA_START_ADDR) {
  1433.     SET_LONG(iopbPtr->bufferAddr, (unsigned)addr);
  1434.     } else {
  1435.     SET_LONG(iopbPtr->bufferAddr, (unsigned)addr - VMMACH_DMA_START_ADDR);
  1436.     }
  1437.     SET_LONG(iopbPtr->maxXferLen, scsiCmdPtr->bufferLen);
  1438.     iopbPtr->cmd.scsiArg.unitAddress = devPtr->unitAddress;
  1439.     bcopy(scsiCmdPtr->commandBlock, (char *) iopbPtr->cmd.scsiArg.cmd, 
  1440.                 scsiCmdPtr->commandBlockLen);
  1441. }
  1442.  
  1443.  
  1444. /*
  1445.  *----------------------------------------------------------------------
  1446.  *
  1447.  * ScsiErrorProc --
  1448.  *
  1449.  *    This function retrieves the Sense data from a device and 
  1450.  *    Unfreezes the queue to the device.
  1451.  *
  1452.  * Results:
  1453.  *    None.
  1454.  *
  1455.  * Side effects:
  1456.  *    A REQUEST SENSE command is sent to the device and the requesting
  1457.  *    call back function called.
  1458.  *
  1459.  *----------------------------------------------------------------------
  1460.  */
  1461. /*ARGSUSED*/
  1462. static void
  1463. ScsiErrorProc(data, callInfoPtr)
  1464.     ClientData          data;
  1465.     Proc_CallInfo       *callInfoPtr;
  1466. {
  1467.     Device    *devPtr = (Device *) data;
  1468.     volatile JaguarIOPB iopbMem;    
  1469.     JaguarCRB    crb;
  1470.     ScsiCmd    senseCmd;
  1471.     char    senseBuffer[DEV_MAX_SENSE_BYTES];
  1472.     DevScsiSenseCmd((ScsiDevice *)devPtr, DEV_MAX_SENSE_BYTES, senseBuffer, 
  1473.             &senseCmd);
  1474.     FillInScsiIOPB(devPtr, &senseCmd, &iopbMem);
  1475.     bzero((char *)&crb, sizeof(crb));
  1476.     MASTER_LOCK(&devPtr->ctrlPtr->mutex);
  1477.     (void) SendJaguarCmd(devPtr->ctrlPtr, 0, &iopbMem, FILL_IN_CRB_ACTION, 
  1478.               (ClientData) &crb);
  1479.     MASTER_UNLOCK(&devPtr->ctrlPtr->mutex);
  1480.     /*
  1481.      * Ignore the 0x34 ( transfer length mismatch) we're likely to get.
  1482.      */
  1483.     if (crb.iopb.returnStatus & 0xff) {
  1484.     crb.iopb.returnStatus &= ~0xff;
  1485.     }
  1486.     if (crb.iopb.returnStatus != 0) {
  1487.     (devPtr->frozen.scsiCmdPtr->doneProc)(devPtr->frozen.scsiCmdPtr, 
  1488.                    SUCCESS, devPtr->frozen.statusByte,
  1489.                    devPtr->frozen.amountTransferred,
  1490.                    0, (char *) 0);
  1491.     } else {
  1492.     (devPtr->frozen.scsiCmdPtr->doneProc)(devPtr->frozen.scsiCmdPtr, 
  1493.                    SUCCESS, devPtr->frozen.statusByte,
  1494.                    devPtr->frozen.amountTransferred,
  1495.                    READ_LONG(crb.iopb.maxXferLen), senseBuffer);
  1496.  
  1497.     }
  1498.     /*
  1499.      * Unfreze the workqueue for this device. 
  1500.      */
  1501.     MASTER_LOCK(&devPtr->ctrlPtr->mutex);
  1502.     devPtr->ctrlPtr->memPtr->mcsb.thawQueue = 
  1503.             THAW_WORK_QUEUE(devPtr->workQueue);
  1504.     MACH_DELAY(100);
  1505.     MASTER_UNLOCK(&devPtr->ctrlPtr->mutex);
  1506. }
  1507.  
  1508.  
  1509. /*
  1510.  *----------------------------------------------------------------------
  1511.  *
  1512.  * RequestDone --
  1513.  *
  1514.  *    Process a request that has finished. Unless a SCSI check condition
  1515.  *    bit is present in the status returned, the request call back
  1516.  *    function is called.  If check condition is set we fire off a
  1517.  *    SCSI REQUEST SENSE to get the error sense bytes from the device.
  1518.  *
  1519.  * Results:
  1520.  *    None.
  1521.  *
  1522.  * Side effects:
  1523.  *    The call back function may be called.
  1524.  *
  1525.  *----------------------------------------------------------------------
  1526.  */
  1527.  
  1528. static void
  1529. RequestDone(devPtr,scsiCmdPtr,status,scsiStatusByte,amountTransferred)
  1530.     Device    *devPtr;    /* Device for request. */
  1531.     ScsiCmd    *scsiCmdPtr;    /* Request that finished. */
  1532.     ReturnStatus status;    /* Status returned. */
  1533.     unsigned char scsiStatusByte;    /* SCSI Status Byte. */
  1534.     int        amountTransferred; /* Amount transferred by command. */
  1535. {
  1536.     if (devJaguarDebug > 3) {
  1537.     printf("RequestDone for %s status 0x%x scsistatus 0x%x count %d\n",
  1538.         devPtr->handle.locationName, status,scsiStatusByte,
  1539.         amountTransferred);
  1540.     }
  1541.     /*
  1542.      * If the request 
  1543.      * suffered an error or the HBA or the scsi status byte
  1544.      * says there is no error sense present, we can do the
  1545.      * callback and free the controller.
  1546.      */
  1547.     if ((status != SUCCESS) || !SCSI_CHECK_STATUS(scsiStatusByte)) {
  1548.     (scsiCmdPtr->doneProc)(scsiCmdPtr, status, scsiStatusByte,
  1549.                    amountTransferred, 0, (char *) 0);
  1550.      return;
  1551.    } 
  1552.    /*
  1553.     * If we got here than the SCSI command came back from the device
  1554.     * with the CHECK bit set in the status byte. We do this with 
  1555.     * a call back process that can wait for workQueue 0 to become
  1556.     * available.
  1557.     */
  1558.    devPtr->frozen.scsiCmdPtr = scsiCmdPtr;
  1559.    devPtr->frozen.statusByte = scsiStatusByte;
  1560.    devPtr->frozen.amountTransferred = amountTransferred;
  1561.    Proc_CallFunc(ScsiErrorProc, (ClientData) devPtr, 0);
  1562. }
  1563.  
  1564. /*
  1565.  *----------------------------------------------------------------------
  1566.  *
  1567.  * ReleaseProc --
  1568.  *
  1569.  *    Release an attached Jaguar device.
  1570.  *
  1571.  * Results:
  1572.  *    None.
  1573.  *
  1574.  * Side effects:
  1575.  *    None.
  1576.  *
  1577.  *----------------------------------------------------------------------
  1578.  */
  1579. /*ARGSUSED*/
  1580. static ReturnStatus
  1581. ReleaseProc(scsiDevicePtr)
  1582.     ScsiDevice    *scsiDevicePtr;
  1583. {
  1584.     return SUCCESS;
  1585. }
  1586.  
  1587.  
  1588.  
  1589. /*
  1590.  *----------------------------------------------------------------------
  1591.  *
  1592.  * entryAvailProc --
  1593.  *
  1594.  *    Act upon an entry becomming available in the queue for this
  1595.  *    controller. This routine is the Dev_Queue callback function that
  1596.  *    is called whenever work becomes available for this controller. 
  1597.  *    If the controller is not already busy we dequeue and start the
  1598.  *    request.
  1599.  *
  1600.  * Results:
  1601.  *    None.
  1602.  *
  1603.  * Side effects:
  1604.  *    Request may be dequeue and submitted to the device. Request callback
  1605.  *    function may be called.
  1606.  *
  1607.  *----------------------------------------------------------------------
  1608.  */
  1609.  
  1610. static Boolean
  1611. entryAvailProc(clientData, newRequestPtr) 
  1612.    ClientData    clientData;    /* Really the Device this request ready. */
  1613.    List_Links *newRequestPtr;    /* The new SCSI request. */
  1614. {
  1615.     register Device *devPtr = (Device *) clientData;
  1616.     register Controller *ctrlPtr = devPtr->ctrlPtr;
  1617.     register ScsiCmd    *scsiCmdPtr = (ScsiCmd *) newRequestPtr;
  1618.     Boolean    good;
  1619.  
  1620.  
  1621. #ifdef ONE_CMD_AT_A_TIME
  1622.     if (ctrlPtr->numActiveCmds >= MAX_CMDS_IN_CONTROLLER) {
  1623.     return FALSE;
  1624.     }
  1625.     ctrlPtr->numActiveCmds++;
  1626. #else
  1627.     if (devPtr->numActiveCmds >= MAX_CMDS_QUEUED) { 
  1628.     return FALSE;
  1629.     }
  1630.     devPtr->numActiveCmds++;
  1631. #endif /* ONE_CMD_AT_A_TIME */
  1632.     good = SendScsiCommand(devPtr, scsiCmdPtr);
  1633.     /*    
  1634.      * If the command couldn't be started do the callback function.
  1635.      */
  1636.     if (!good) {
  1637. #ifdef ONE_CMD_AT_A_TIME
  1638.     ctrlPtr->numActiveCmds--;
  1639. #else
  1640.      devPtr->numActiveCmds--;
  1641. #endif /* ONE_CMD_AT_A_TIME */
  1642.      MASTER_UNLOCK(&(ctrlPtr->mutex));
  1643.      RequestDone(devPtr,scsiCmdPtr,(ReturnStatus)DEV_HARD_ERROR,0,0);
  1644.      MASTER_LOCK(&(ctrlPtr->mutex));
  1645.     }
  1646.     return TRUE;
  1647.  
  1648. }   
  1649.  
  1650. /*
  1651.  *----------------------------------------------------------------------
  1652.  *
  1653.  * StartNextRequest --
  1654.  *
  1655.  *    Start the next request on the device. 
  1656.  *
  1657.  * Results:
  1658.  *    None.
  1659.  *
  1660.  * Side effects:
  1661.  *    None.
  1662.  *
  1663.  *----------------------------------------------------------------------
  1664.  */
  1665.  
  1666. static void
  1667. StartNextRequest(devPtr)
  1668.     Device    *devPtr;    /* Device to start request on. */
  1669. {
  1670.     List_Links    *newRequest;
  1671.  
  1672. #ifdef ONE_CMD_AT_A_TIME
  1673.     while (devPtr->ctrlPtr->numActiveCmds < MAX_CMDS_IN_CONTROLLER) { 
  1674.     ClientData    clientData;
  1675.     newRequest = Dev_QueueGetNextFromSet(devPtr->ctrlPtr->devQueues,
  1676.                 DEV_QUEUE_ANY_QUEUE_MASK,&clientData);
  1677.     if (newRequest == (List_Links *) NIL) {
  1678.         break;
  1679.     }
  1680.     (void) entryAvailProc((ClientData) clientData, newRequest);
  1681.     }
  1682. #else
  1683.     while (devPtr->numActiveCmds < MAX_CMDS_QUEUED) { 
  1684.     newRequest = Dev_QueueGetNext(devPtr->handle.devQueue);
  1685.     if (newRequest == (List_Links *) NIL) {
  1686.         break;
  1687.     }
  1688.     (void) entryAvailProc((ClientData) devPtr, newRequest);
  1689.     }
  1690. #endif /* ONE_CMD_AT_A_TIME */
  1691. }
  1692.  
  1693. /*
  1694.  *----------------------------------------------------------------------
  1695.  *
  1696.  * DevJaguarInit --
  1697.  *
  1698.  *    Check for the existant of the Jaguar HBA controller. If it
  1699.  *    exists allocate data stuctures for it.
  1700.  *
  1701.  * Results:
  1702.  *    TRUE if the controller exists, FALSE otherwise.
  1703.  *
  1704.  * Side effects:
  1705.  *    Memory may be allocated.
  1706.  *
  1707.  *----------------------------------------------------------------------
  1708.  */
  1709. ClientData
  1710. DevJaguarInit(ctrlLocPtr)
  1711.     DevConfigController    *ctrlLocPtr;    /* Controller location. */
  1712. {
  1713.     int    ctrlNum;
  1714.     Controller *ctrlPtr;
  1715.     short    x;
  1716.     int        i;
  1717.     Address    address;
  1718.     ReturnStatus status;
  1719.  
  1720.     /*
  1721.      * See if the controller is there. This controller should occupy
  1722.      * 2k in the short IO space of the VME.
  1723.      */
  1724.     if (ctrlLocPtr->space != DEV_VME_D16A16) {
  1725.     panic("Jaguar SCSI HBA %d configured in bad address space %d @ 0x%x\n",
  1726.           ctrlLocPtr->controllerID, ctrlLocPtr->space, ctrlLocPtr->address);
  1727.     return DEV_NO_CONTROLLER;
  1728.     }
  1729.  
  1730.     address =    (Address) ctrlLocPtr->address;
  1731.     status = Mach_Probe(sizeof(short), address, (char *)&x);
  1732.     if (status == SUCCESS) {
  1733.     status = Mach_Probe(sizeof(short), address + 2*1024 - 2,(char *)&x);
  1734.     }
  1735.     if (status != SUCCESS) {
  1736.     if (devJaguarDebug > 3) {
  1737.         printf("Jaguar # %d not found at address 0x%x\n",
  1738.            ctrlLocPtr->controllerID, address);
  1739.     }
  1740.     return DEV_NO_CONTROLLER;
  1741.     }
  1742.     if (!CheckSizes()) {
  1743.     panic("Jaguar driver structure layout broken\n");
  1744.     return DEV_NO_CONTROLLER;
  1745.     }
  1746.     ctrlNum = ctrlLocPtr->controllerID;
  1747.     { 
  1748.     Boolean    good;
  1749.     good = InitializeJaguar((volatile JaguarMem *) address,
  1750.                          ctrlLocPtr->name,
  1751.                  VME_INTERRUPT_PRIORITY,
  1752.                  ctrlLocPtr->vectorNumber);
  1753.     if (!good) {
  1754.         return DEV_NO_CONTROLLER;
  1755.     }
  1756.     }
  1757.     ctrlPtr = Controllers[ctrlNum] = (Controller *) malloc(sizeof(*ctrlPtr));
  1758.     bzero((char *) ctrlPtr, sizeof(Controller));
  1759.     ctrlPtr->memPtr = (JaguarMem *) address;
  1760.     ctrlPtr->nextCQE = ctrlPtr->memPtr->cmdQueue;
  1761.     ctrlPtr->workQueue0Busy = FALSE;
  1762. #ifdef ONE_CMD_AT_A_TIME
  1763.     ctrlPtr->numActiveCmds = 0;
  1764. #endif /* ONE_CMD_AT_A_TIME */
  1765.  
  1766.     ctrlPtr->name = ctrlLocPtr->name;
  1767.     Sync_SemInitDynamic(&(ctrlPtr->mutex),ctrlPtr->name);
  1768.     /* 
  1769.      * Initialized the name, device queue header, and the master lock.
  1770.      * The controller comes up with no devices active and no devices
  1771.      * attached.  
  1772.      */
  1773.     ctrlPtr->devQueues = Dev_CtrlQueuesCreate(&(ctrlPtr->mutex),entryAvailProc);
  1774.     ctrlPtr->intrLevel = VME_INTERRUPT_PRIORITY;
  1775.     /*
  1776.      * Use the same vector for both error and normal completion.
  1777.      */
  1778.     ctrlPtr->intrVector = 
  1779.     JAGUAR_IOPB_INTR_VECTOR(ctrlLocPtr->vectorNumber,
  1780.                 ctrlLocPtr->vectorNumber);
  1781.     for (i = 0; i < NUM_WORK_QUEUES; i++) {
  1782.     ctrlPtr->devices[i] = (Device *) NIL;
  1783.     }
  1784.  
  1785.     return (ClientData) ctrlPtr;
  1786. }
  1787.  
  1788.  
  1789. /*
  1790.  *----------------------------------------------------------------------
  1791.  *
  1792.  * DevJaguarAttachDevice --
  1793.  *
  1794.  *    Attach a SCSI device using the Jaguar HBA. 
  1795.  *
  1796.  * Results:
  1797.  *    None.
  1798.  *
  1799.  * Side effects:
  1800.  *    None.
  1801.  *
  1802.  *----------------------------------------------------------------------
  1803.  */
  1804.  
  1805. ScsiDevice   *
  1806. DevJaguarAttachDevice(devicePtr, insertProc)
  1807.     Fs_Device    *devicePtr;     /* Device to attach. */
  1808.     void (*insertProc) _ARGS_ ((List_Links *elementPtr,
  1809.                                 List_Links *elementListHdrPtr));
  1810.                                  /* Queue insert procedure. */
  1811. {
  1812.     Device *devPtr;
  1813.     Controller    *ctrlPtr;
  1814.     char   tmpBuffer[512];
  1815.     int       ctrlNum;
  1816.     int       targetID, lun, bus;
  1817.     int       i, workQueue;
  1818.  
  1819.     /*
  1820.      * First find the Jaguar controller this device is on. For the Jaguar
  1821.      * we really have 2 HBA per real controller because each HBA has 
  1822.      * two SCSI buses.
  1823.      */
  1824.     ctrlNum = SCSI_HBA_NUMBER(devicePtr)/2;
  1825.     if ((ctrlNum > MAX_JAGUAR_CTRLS) ||
  1826.     (Controllers[ctrlNum] == (Controller *) 0)) { 
  1827.     return (ScsiDevice  *) NIL;
  1828.     } 
  1829.     ctrlPtr = Controllers[ctrlNum];
  1830.     /*
  1831.      * See if the device is already present.
  1832.      */
  1833.     targetID = SCSI_TARGET_ID(devicePtr);
  1834.     lun = SCSI_LUN(devicePtr);
  1835.     bus = SCSI_HBA_NUMBER(devicePtr) & 0x1;
  1836.     MASTER_LOCK(&(ctrlPtr->mutex));
  1837.     /*
  1838.      * See if we already have a work queue setup for this device. Also,
  1839.      * find a unsed workQueue to allocate for this device.
  1840.      */
  1841.     workQueue = -1;
  1842.     for (i = 0; i < NUM_WORK_QUEUES; i++) {
  1843.     if (ctrlPtr->devices[i] != (Device *) NIL) {
  1844.         if ((ctrlPtr->devices[i]->targetID == targetID) &&
  1845.             (ctrlPtr->devices[i]->bus == bus)) {
  1846.         if (ctrlPtr->devices[i]->handle.LUN == lun) {
  1847.             MASTER_UNLOCK(&(ctrlPtr->mutex));
  1848.             return (ScsiDevice  *) (ctrlPtr->devices[i]);
  1849.         }
  1850.         /*
  1851.          * The same targetID and a different LUN doesn't work.
  1852.          */
  1853.         MASTER_UNLOCK(&(ctrlPtr->mutex));
  1854.         printf("Warning: %s: 4210 only supports one LUN per target.\n",
  1855.                ctrlPtr->name);
  1856.         printf("%s: Target %d LUN %d is already present.\n", 
  1857.               ctrlPtr->name,
  1858.               ctrlPtr->devices[i]->targetID, 
  1859.               ctrlPtr->devices[i]->handle.LUN);
  1860.         return (ScsiDevice *) NIL;
  1861.         }
  1862.     } else {
  1863.         /*
  1864.          * Record the first unsed workQueue.
  1865.          */
  1866.         if (workQueue == -1) {
  1867.         workQueue = i+1;
  1868.         }
  1869.     }
  1870.     }
  1871.     MASTER_UNLOCK(&(ctrlPtr->mutex));
  1872.     if (workQueue == -1) {
  1873.     printf("%s: Too many devices attached.\n", ctrlPtr->name);
  1874.     return (ScsiDevice *) NIL;
  1875.     }
  1876.  
  1877.     if (!InitializeWorkq(ctrlPtr, workQueue, FALSE, 1)) {
  1878.     return (ScsiDevice *) NIL;
  1879.     }
  1880.  
  1881.     ctrlPtr->devices[workQueue-1] = devPtr =
  1882.                     (Device *) malloc(sizeof(Device));
  1883.     bzero((char *) devPtr, sizeof(Device));
  1884. #ifdef ONE_CMD_AT_A_TIME
  1885.     devPtr->handle.devQueue = Dev_QueueCreate(ctrlPtr->devQueues,
  1886.                 1, insertProc, (ClientData) devPtr);
  1887. #else
  1888.     devPtr->handle.devQueue = Dev_QueueCreate(ctrlPtr->devQueues,
  1889.                 0, insertProc, (ClientData) devPtr);
  1890. #endif
  1891.     (void) sprintf(tmpBuffer, "%s#%d Bus %d Target %d LUN %d", ctrlPtr->name, 
  1892.             ctrlNum, bus, targetID, lun);
  1893.     devPtr->handle.locationName = 
  1894.         (char *) strcpy(malloc(strlen(tmpBuffer)+1),tmpBuffer);
  1895.     devPtr->handle.LUN = lun;
  1896.     devPtr->handle.releaseProc = ReleaseProc;
  1897.     devPtr->handle.maxTransferSize = DEV_MAX_DMA_SIZE;
  1898.  
  1899.     devPtr->bus = bus;
  1900.     devPtr->targetID = targetID;
  1901.     devPtr->unitAddress = JAGUAR_UNIT_ADDRESS(bus, targetID, lun);
  1902.     devPtr->workQueue = workQueue;
  1903.     devPtr->ctrlPtr = ctrlPtr;
  1904.     return (ScsiDevice *) devPtr;
  1905. }
  1906.  
  1907.